package com.nanyang.tms.api.dto.request.cost.add;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.nanyang.common.core.dict.CommonEnum;
import com.nanyang.common.core.domain.ApiRequest;
import com.nanyang.common.core.exception.ValidateException;
import lombok.Data;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.List;

@Data
public class CostBaseAddRequest extends ApiRequest {
    @JsonIgnore
    private String pin;
    @Valid
    @NotNull(message = "costInfoAddRequest不能为空")
    private CostInfoAddRequest costInfoAddRequest;
    @Valid
    @NotNull(message = "costPartSchemeAddRequest不能为空")
    private CostPartSchemeAddRequest costPartSchemeAddRequest;
    @Valid
    @NotNull(message = "costWeightAddRequest不能为空")
    private CostWeightAddRequest costWeightAddRequest;
    @Valid
    @NotNull(message = "costPriceRecordAddRequestList不能为空")
    private List<CostPriceRecordAddRequest> costPriceRecordAddRequestList;
    @Override
    public void requestCheck() {
        List<CostWeightRecordAddRequest> costWeightRecordAddRequestList = costWeightAddRequest.getCostWeightRecordAddRequestList();
            BigDecimal[][] weightArray= new BigDecimal[costWeightRecordAddRequestList.size()][2];
            for (int i = 0; i < costWeightRecordAddRequestList.size(); i++) {
                weightArray[i][0] = costWeightRecordAddRequestList.get(i).getWeightStart();
                weightArray[i][1] = costWeightRecordAddRequestList.get(i).getWeightEnd();
                String chargingName = costWeightRecordAddRequestList.get(i).getChargingName();
                if("续单价".equals(chargingName)){
                    if(i==0){
                        throw new ValidateException(CommonEnum.ARG_NOTVALID.getCode(), "重量模板明细 续单价之前必须是总价！");
                    }
                    String frontCharging = costWeightRecordAddRequestList.get(i-1).getChargingName();
                    if(!"总价".equals(frontCharging)){
                        throw new ValidateException(CommonEnum.ARG_NOTVALID.getCode(), "重量模板明细 续单价之前必须是总价！");
                    }
                }
                if("续阶单价".equals(chargingName)){
                    if(i==0){
                        throw new ValidateException(CommonEnum.ARG_NOTVALID.getCode(), "重量模板明细 续阶单价之前必须是总价或续阶单价！");
                    }
                    String frontCharging = costWeightRecordAddRequestList.get(i-1).getChargingName();
                    if("单价".equals(frontCharging)||"续单价".equals(frontCharging)){
                        throw new ValidateException(CommonEnum.ARG_NOTVALID.getCode(), "重量模板明细 续阶单价之前必须是总价或续阶单价！");
                    }
                }
            }
            checkArray(weightArray);

        }

        private void checkArray(BigDecimal[][] array){
            if (0!=array[0][0].doubleValue()) {
                throw new ValidateException(CommonEnum.ARG_NOTVALID.getCode(), "不是以0开头，该区间为不连续区间");
            }
            for (int k = 0; k < array.length - 1; k++) {
                if (array[k][1].compareTo(array[k + 1][0])!=0) {
                    throw new ValidateException(CommonEnum.ARG_NOTVALID.getCode(),"该区间为不连续区间");
                }
            }
        }


}
